package com.coffee.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.common.service.RedisService;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.entity.SiteContextThreadLocal;
import com.coffee.core.util.DateUtil;
import com.coffee.core.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
	private final static Logger LOG = LoggerFactory.getLogger(BaseController.class);

	@DubboReference(check = false)
    private RedisService redisService;

    public LoginUser getSupplier() {
        String stoken=SiteContextThreadLocal.get().getStoken();
        if(StringUtils.isEmpty(stoken)) {
            return null;
        }
        String loginUserStr=(String)redisService.get(stoken);
        LoginUser loginUser =JSONObject.parseObject(loginUserStr,LoginUser.class);

        return loginUser;
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.chageTime(1683859500000L));
    }
    

    public Integer getIntAttr(HttpServletRequest request, String name, Integer defaultValue){
        String value = WebUtil.getParameter(request, name);
        if(StringUtils.isEmpty(value) || !StringUtils.isNumeric(value)){
            return defaultValue;
        }
        return Integer.parseInt(value);
    }

    public PagerInfo getPageInfo(HttpServletRequest request, Integer pageIndex, Integer pageSize) {
        pageIndex = getIntAttr(request, "pageIndex", pageIndex);
        pageSize = getIntAttr(request, "pageSize", pageSize);
        return new PagerInfo(pageSize, pageIndex);
    }

    public PagerInfo setDefaultPageInfo(Integer pageSize, Integer pageIndex) {
        if (pageIndex == null || pageSize == null) {
            return new PagerInfo(10, 1);
        } else {
            return new PagerInfo(pageSize, pageIndex);
        }
    }

}
